<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Scheduler</title>
<link href="tabs.css" rel="stylesheet" type="text/css" />
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="alias.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="alias.js"></script>

<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>


<script type="text/javascript">
  jQuery(document).ready(function () {
    if(gref){ // Number all _img and _table classes
      gref();
    }
  });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->

<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">nRF51822 BLE SDK
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('group__app__scheduler.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="summary">
<a href="#typedef-members">Typedefs</a> &#124;
<a href="#func-members">Functions</a>  </div>
  <div class="headertitle">
<div class="title">Scheduler</div>  </div>
<div class="ingroups"><a class="el" href="group__ble__sdk__lib.html">Bluetooth Libraries</a></div></div>
<div class="contents">

<p>The scheduler is used for transferring execution from the interrupt context to the main context.  
<a href="#details">More...</a></p>
<table class="memberdecls">
<tr><td colspan="2"><h2><a name="typedef-members"></a>
Typedefs</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top"><a class="anchor" id="ga30ec1fc5e48b1481da4f6ca2b24f9aef"></a><!-- doxytag: member="app_scheduler::app_sched_event_handler_t" ref="ga30ec1fc5e48b1481da4f6ca2b24f9aef" args=")(app_sched_event_data_t *p_app_sched_event_data)" -->
typedef void(*&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__app__scheduler.html#ga30ec1fc5e48b1481da4f6ca2b24f9aef">app_sched_event_handler_t</a> )(app_sched_event_data_t *p_app_sched_event_data)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Scheduler event handler type. <br/></td></tr>
<tr><td colspan="2"><h2><a name="func-members"></a>
Functions</h2></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__app__scheduler.html#ga62e8131bc306582c34e1db7565f08a81">app_sched_init</a> (void)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Initializes the Scheduler.  <a href="#ga62e8131bc306582c34e1db7565f08a81"></a><br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__app__scheduler.html#ga6fd6a3db7526418f796c55c096360ded">app_sched_schedule</a> (void)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Executes all scheduled events.  <a href="#ga6fd6a3db7526418f796c55c096360ded"></a><br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">uint32_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__app__scheduler.html#gaaecab81ed0f2e9c1ce56586367b93dc5">app_sched_event_put</a> (app_sched_event_data_t *p_data, <a class="el" href="group__app__scheduler.html#ga30ec1fc5e48b1481da4f6ca2b24f9aef">app_sched_event_handler_t</a> handler, uint8_t queue_id)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Schedules an event.  <a href="#gaaecab81ed0f2e9c1ce56586367b93dc5"></a><br/></td></tr>
<tr><td class="memItemLeft" align="right" valign="top">uint32_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__app__scheduler.html#gad09181e10ae224ca354b5e650b5e3a0a">app_sched_timer_event_schedule</a> (app_timer_id_t timer_id, <a class="el" href="group__app__timer.html#ga0586b27407494731cad5b72b12aa5bfe">app_timer_timeout_handler_t</a> timeout_handler, uint8_t priority)</td></tr>
<tr><td class="mdescLeft">&#160;</td><td class="mdescRight">Schedules a timer event.  <a href="#gad09181e10ae224ca354b5e650b5e3a0a"></a><br/></td></tr>
</table>
<hr/><a name="details" id="details"></a><h2>Detailed Description</h2>
<p>The scheduler is used for transferring execution from the interrupt context to the main context. </p>
<p>See <a class="el" href="group__ble__sdk__apps__seq__diagrams.html">'Sequence Diagrams'</a> for sequence diagrams illustrating the flow of events when using the Scheduler.</p>
<h2><a class="anchor" id="app_scheduler_req"></a>
Requirements:</h2>
<h3><a class="anchor" id="create_app_scheduler_cfg"></a>
Creation of app_scheduler_cfg.h:</h3>
<ul>
<li>Applications using the Scheduler must have a configuration file named app_scheduler_cfg.h (see <a class="el" href="group__ble__sdk__apps__config__files.html">'Config Files'</a> for details).</li>
</ul>
<h3><a class="anchor" id="main_context_logic"></a>
Logic in main context:</h3>
<ul>
<li>Define an event handler for each type of event expected.</li>
<li>Initialise the scheduler by calling <a class="el" href="group__app__scheduler.html#ga62e8131bc306582c34e1db7565f08a81" title="Initializes the Scheduler.">app_sched_init()</a> function before entering the application main loop.</li>
<li>Call <a class="el" href="group__app__scheduler.html#ga6fd6a3db7526418f796c55c096360ded" title="Executes all scheduled events.">app_sched_schedule()</a> from the main loop each time the application wakes up because of an event (typically when <a class="el" href="group__nrf__soc__api.html#ga2aae5f388f1b6179058c62e2fb05a547" title="Waits for an application event.">nrf_wait_for_app_event()</a> returns).</li>
</ul>
<h3><a class="anchor" id="int_context_logic"></a>
Logic in interrupt context:</h3>
<ul>
<li>In the interrupt handler, call <a class="el" href="group__app__scheduler.html#gaaecab81ed0f2e9c1ce56586367b93dc5" title="Schedules an event.">app_sched_event_put()</a> with the appropriate data and event handler. This will insert an event into the scheduler's queue. The <a class="el" href="group__app__scheduler.html#ga6fd6a3db7526418f796c55c096360ded" title="Executes all scheduled events.">app_sched_schedule()</a> function will pull this event and call its handler in the main context.</li>
</ul>
<p>For an example usage of the scheduler, please see the implementations of <a class="el" href="group__ble__sdk__app__hids__mouse.html">HID Mouse Application</a> and <a class="el" href="group__ble__sdk__app__hids__keyboard.html">HID Keyboard Application</a>.</p>
<div class="image">
<img src="scheduler_working.jpg" alt="scheduler_working.jpg"/>
<div class="caption">
The high level design of the scheduler</div></div>
 <hr/><h2>Function Documentation</h2>
<a class="anchor" id="ga62e8131bc306582c34e1db7565f08a81"></a><!-- doxytag: member="app_scheduler.h::app_sched_init" ref="ga62e8131bc306582c34e1db7565f08a81" args="(void)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void app_sched_init </td>
          <td>(</td>
          <td class="paramtype">void&#160;</td>
          <td class="paramname"></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>Initializes the Scheduler. </p>
<p>This initializes the event queues. It must be called before entering the main loop. </p>

<p>Definition at line <a class="el" href="app__scheduler_8c_source.html#l00058">58</a> of file <a class="el" href="app__scheduler_8c_source.html">app_scheduler.c</a>.</p>

</div>
</div>
<a class="anchor" id="ga6fd6a3db7526418f796c55c096360ded"></a><!-- doxytag: member="app_scheduler.h::app_sched_schedule" ref="ga6fd6a3db7526418f796c55c096360ded" args="(void)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">void app_sched_schedule </td>
          <td>(</td>
          <td class="paramtype">void&#160;</td>
          <td class="paramname"></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>Executes all scheduled events. </p>
<p>This function must be called from within the main loop. It will execute all events in all queues. </p>

<p>Definition at line <a class="el" href="app__scheduler_8c_source.html#l00098">98</a> of file <a class="el" href="app__scheduler_8c_source.html">app_scheduler.c</a>.</p>

</div>
</div>
<a class="anchor" id="gaaecab81ed0f2e9c1ce56586367b93dc5"></a><!-- doxytag: member="app_scheduler.h::app_sched_event_put" ref="gaaecab81ed0f2e9c1ce56586367b93dc5" args="(app_sched_event_data_t *p_data, app_sched_event_handler_t handler, uint8_t queue_id)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">uint32_t app_sched_event_put </td>
          <td>(</td>
          <td class="paramtype">app_sched_event_data_t *&#160;</td>
          <td class="paramname"><em>p_data</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype"><a class="el" href="group__app__scheduler.html#ga30ec1fc5e48b1481da4f6ca2b24f9aef">app_sched_event_handler_t</a>&#160;</td>
          <td class="paramname"><em>handler</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">uint8_t&#160;</td>
          <td class="paramname"><em>queue_id</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>Schedules an event. </p>
<p>Puts an event into an event queue.</p>
<dl><dt><b>Parameters:</b></dt><dd>
  <table class="params">
    <tr><td class="paramdir">[in]</td><td class="paramname">p_data</td><td>Pointer to event structure to be sent. </td></tr>
    <tr><td class="paramdir">[in]</td><td class="paramname">handler</td><td>Event handler to receive the event. </td></tr>
    <tr><td class="paramdir">[in]</td><td class="paramname">queue_id</td><td>ID of the queue to put the event. </td></tr>
  </table>
  </dd>
</dl>
<dl class="return"><dt><b>Returns:</b></dt><dd>NRF_SUCCESS on success, otherwise an error code. </dd></dl>

<p>Definition at line <a class="el" href="app__scheduler_8c_source.html#l00119">119</a> of file <a class="el" href="app__scheduler_8c_source.html">app_scheduler.c</a>.</p>

</div>
</div>
<a class="anchor" id="gad09181e10ae224ca354b5e650b5e3a0a"></a><!-- doxytag: member="app_scheduler.h::app_sched_timer_event_schedule" ref="gad09181e10ae224ca354b5e650b5e3a0a" args="(app_timer_id_t timer_id, app_timer_timeout_handler_t timeout_handler, uint8_t priority)" -->
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">uint32_t app_sched_timer_event_schedule </td>
          <td>(</td>
          <td class="paramtype">app_timer_id_t&#160;</td>
          <td class="paramname"><em>timer_id</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype"><a class="el" href="group__app__timer.html#ga0586b27407494731cad5b72b12aa5bfe">app_timer_timeout_handler_t</a>&#160;</td>
          <td class="paramname"><em>timeout_handler</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">uint8_t&#160;</td>
          <td class="paramname"><em>priority</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div>
<div class="memdoc">

<p>Schedules a timer event. </p>
<p>Puts an timer event into the event queue. A pointer to this function is passed to app_sched_timers_init() to make the timer module pass timer events to the event scheduler.</p>
<dl><dt><b>Parameters:</b></dt><dd>
  <table class="params">
    <tr><td class="paramdir">[in]</td><td class="paramname">timer_id</td><td>Id of timer on which an event occurred. </td></tr>
    <tr><td class="paramdir">[in]</td><td class="paramname">timeout_handler</td><td>Application timeout handler. </td></tr>
    <tr><td class="paramdir">[in]</td><td class="paramname">priority</td><td>Priority level at which the timeout handler will be run. This will be mapped directly to the scheduler queue_id. The highest priority is 0, the second highest is 1 and so on. </td></tr>
  </table>
  </dd>
</dl>
<dl class="return"><dt><b>Returns:</b></dt><dd>NRF_SUCCESS on success, otherwise an error code. </dd></dl>

<p>Definition at line <a class="el" href="app__scheduler_8c_source.html#l00157">157</a> of file <a class="el" href="app__scheduler_8c_source.html">app_scheduler.c</a>.</p>

</div>
</div>
</div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>


    <li class="footer">
      Copyright &copy 2006-2011 <a href="http://www.nordicsemi.no" style="text-decoration:none">Nordic Semiconductor</a>.
      All Rights Reserved.
      <a href="disclaimer.html">Disclaimer</a>
    </li>
   </ul>
 </div>


</body>
</html>
